var express = require('express');
var router = express.Router();
const { getList, getDetail, createBlog, editBlog, deleteBlog } = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { currentTime } = require('../util/currentTime')
const { loginCheck } = require('../util/loginCheck')

router.get('/list', function(req, res, next) {
    if (loginCheck(req)) {
        return loginCheck(req).then(data => {
            res.json(
                new ErrorModel(data, '博客列表')
            )
        })
    }
    const author = req.query.author
    const keyword = req.query.keyword
    const promise = getList(author, keyword)
    promise.then((sqlData) => {
        res.json(
            new SuccessModel(sqlData, '博客列表')
        )
    })
});

router.get('/detail', function(req, res, next) {
    const id = req.query.id
    const promise = getDetail(id)
    promise.then((sqlData) => {
        if (sqlData.length > 0) {
            res.json(
                new SuccessModel(sqlData[0], '博客详情')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '未查询到相关博客',
                    failTime: currentTime()
                }, '博客详情')
            )
        }
    })
});

router.post('/create', function(req, res, next) {
    const title = req.body.title
    const content = req.body.content
    const author = req.body.author
    const promise = createBlog(title, content, author, currentTime(), currentTime())
    promise.then((sqlData) => {
        if (sqlData.insertId && sqlData.affectedRows) {
            res.json(
                new SuccessModel({
                    tip: '创建成功',
                    createTime: currentTime()
                }, '创建博客')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '创建失败',
                    failTime: currentTime()
                }, '创建博客')
            )
        }  
    })
});

router.post('/edit', function(req, res, next) {
    const title = req.body.title
    const content = req.body.content
    const author = req.body.author
    const id = req.query.id
    const promise = editBlog(title, content, author, currentTime(), id)
    promise.then((sqlData) => {
        if (sqlData.affectedRows) {
            res.json(
                new SuccessModel({
                    tip: '编辑成功',
                    updateTime: currentTime()
                }, '编辑博客')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '编辑失败',
                    failTime: currentTime()
                }, '编辑博客')
            )
        }  
    })
});

router.post('/delete', function(req, res, next) {
    const id = req.query.id
    const promise = deleteBlog(id)
    promise.then((sqlData) => {
        if (sqlData.affectedRows) {
            res.json(
                new SuccessModel({
                    tip: '删除成功',
                    updateTime: currentTime()
                }, '删除博客')
            )
        } else {
            res.json(
                new ErrorModel({
                    tip: '删除失败',
                    failTime: currentTime()
                }, '删除博客')
            )
        }  
    })
});

module.exports = router;
